package com.feeyin.radar.crm.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.multipart.MultipartFile;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.FileUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.ContractDAO;
import com.feeyin.radar.crm.dao.ContractInvoiceDAO;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.dao.CustomerShareDAO;
import com.feeyin.radar.crm.dao.CustomerUpdateHistoryDAO;
import com.feeyin.radar.crm.dao.LinkmanDAO;
import com.feeyin.radar.crm.dao.SaleCostDAO;
import com.feeyin.radar.crm.dao.SaleRecordDAO;
import com.feeyin.radar.crm.dao.SystemFileDAO;
import com.feeyin.radar.crm.form.CustomerLimitForm;
import com.feeyin.radar.crm.model.ContractCaseType;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.model.CustomerShare;
import com.feeyin.radar.crm.model.ProcessGroup;
import com.feeyin.radar.crm.model.QueryCustomer;
import com.feeyin.radar.crm.model.SaleRecord;
import com.feeyin.radar.crm.model.SystemFile;
import com.feeyin.radar.crm.service.CustomerService;
import com.feeyin.radar.sys.dao.CountryDAO;
import com.feeyin.radar.sys.dao.DataDictionaryDAO;
import com.feeyin.radar.sys.dao.DepartmentDAO;
import com.feeyin.radar.sys.dao.ManageSetDAO;
import com.feeyin.radar.sys.dao.RecordRangeDAO;
import com.feeyin.radar.sys.dao.SaleStageSetDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.DataDictionary;
import com.feeyin.radar.sys.model.ManageSet;
import com.feeyin.radar.sys.model.ManageSetObject;
import com.feeyin.radar.sys.model.SaleStageSet;
import com.feeyin.radar.sys.model.Users;

public class CustomerServiceImpl
  implements CustomerService
{
  private CustomerDAO customerDAO;
  private DataDictionaryDAO dataDictionaryDAO;
  private RecordRangeDAO recordRangeDAO;
  private CountryDAO countryDAO;
  private ManageSetDAO manageSetDAO;
  private UserDAO userDAO;
  private CustomerShareDAO customerShareDAO;
  private DepartmentDAO departmentDAO;
  private CustomerUpdateHistoryDAO customerUpdateHistoryDAO;
  private SaleRecordDAO saleRecordDAO;
  private SaleStageSetDAO saleStageSetDAO;
  private ContractDAO contractDAO;
  private ContractInvoiceDAO contractInvoiceDAO;
  private SaleCostDAO saleCostDAO;
  private LinkmanDAO linkmanDAO;
  private SystemFileDAO systemFileDAO;

  public CustomerDAO getCustomerDAO()
  {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public DataDictionaryDAO getDataDictionaryDAO() {
    return this.dataDictionaryDAO;
  }

  public void setDataDictionaryDAO(DataDictionaryDAO dataDictionaryDAO) {
    this.dataDictionaryDAO = dataDictionaryDAO;
  }

  public RecordRangeDAO getRecordRangeDAO() {
    return this.recordRangeDAO;
  }

  public void setRecordRangeDAO(RecordRangeDAO recordRangeDAO) {
    this.recordRangeDAO = recordRangeDAO;
  }

  public CountryDAO getCountryDAO() {
    return this.countryDAO;
  }

  public void setCountryDAO(CountryDAO countryDAO) {
    this.countryDAO = countryDAO;
  }

  public ManageSetDAO getManageSetDAO() {
    return this.manageSetDAO;
  }

  public void setManageSetDAO(ManageSetDAO manageSetDAO) {
    this.manageSetDAO = manageSetDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public CustomerShareDAO getCustomerShareDAO() {
    return this.customerShareDAO;
  }

  public void setCustomerShareDAO(CustomerShareDAO customerShareDAO) {
    this.customerShareDAO = customerShareDAO;
  }

  public DepartmentDAO getDepartmentDAO() {
    return this.departmentDAO;
  }

  public void setDepartmentDAO(DepartmentDAO departmentDAO) {
    this.departmentDAO = departmentDAO;
  }

  public CustomerUpdateHistoryDAO getCustomerUpdateHistoryDAO() {
    return this.customerUpdateHistoryDAO;
  }

  public void setCustomerUpdateHistoryDAO(CustomerUpdateHistoryDAO customerUpdateHistoryDAO)
  {
    this.customerUpdateHistoryDAO = customerUpdateHistoryDAO;
  }

  public SaleRecordDAO getSaleRecordDAO() {
    return this.saleRecordDAO;
  }

  public void setSaleRecordDAO(SaleRecordDAO saleRecordDAO) {
    this.saleRecordDAO = saleRecordDAO;
  }

  public SaleStageSetDAO getSaleStageSetDAO() {
    return this.saleStageSetDAO;
  }

  public void setSaleStageSetDAO(SaleStageSetDAO saleStageSetDAO) {
    this.saleStageSetDAO = saleStageSetDAO;
  }

  public ContractDAO getContractDAO() {
    return this.contractDAO;
  }

  public void setContractDAO(ContractDAO contractDAO) {
    this.contractDAO = contractDAO;
  }

  public ContractInvoiceDAO getContractInvoiceDAO() {
    return this.contractInvoiceDAO;
  }

  public void setContractInvoiceDAO(ContractInvoiceDAO contractInvoiceDAO) {
    this.contractInvoiceDAO = contractInvoiceDAO;
  }

  public SaleCostDAO getSaleCostDAO() {
    return this.saleCostDAO;
  }

  public void setSaleCostDAO(SaleCostDAO saleCostDAO) {
    this.saleCostDAO = saleCostDAO;
  }

  public LinkmanDAO getLinkmanDAO() {
    return this.linkmanDAO;
  }

  public void setLinkmanDAO(LinkmanDAO linkmanDAO) {
    this.linkmanDAO = linkmanDAO;
  }

  public SystemFileDAO getSystemFileDAO() {
    return this.systemFileDAO;
  }

  public void setSystemFileDAO(SystemFileDAO systemFileDAO) {
    this.systemFileDAO = systemFileDAO;
  }

  public Map getCustomerInfoRecords() {
    Map map = new HashMap();

    List customerDictionarys = new ArrayList();
    customerDictionarys = this.dataDictionaryDAO.getDataDictionarysByRecordType("1");
    if (customerDictionarys != null) {
      for (int i = 0; i < customerDictionarys.size(); i++) {
        DataDictionary dataDictionary = new DataDictionary();
        dataDictionary = (DataDictionary)customerDictionarys.get(i);
        map.put(dataDictionary.getRecordCode(), dataDictionary);
      }
    }
    return map;
  }

  public Map getCustomerDictionaryRanges() {
    Map map = new HashMap();

    List customerDictionarys = new ArrayList();
    customerDictionarys = this.dataDictionaryDAO.getDataDictionarysByRecordType("1");
    if (customerDictionarys != null) {
      for (int i = 0; i < customerDictionarys.size(); i++) {
        DataDictionary dataDictionary = new DataDictionary();
        dataDictionary = (DataDictionary)customerDictionarys.get(i);
        List ranges = this.recordRangeDAO.getActivityRecordRangesByDictionaryId(dataDictionary.getId());
        if ("124".equals(dataDictionary.getRecordCode()))
          ranges = this.countryDAO.getCountrysByParentId(Integer.valueOf(0));
        else if ("125".equals(dataDictionary.getRecordCode()))
          ranges = this.countryDAO.getCountrysByLevel(Integer.valueOf(2));
        else if ("126".equals(dataDictionary.getRecordCode()))
          ranges = this.countryDAO.getCountrysByLevel(Integer.valueOf(3));
        else if ("127".equals(dataDictionary.getRecordCode())) {
          ranges = this.recordRangeDAO.getActivityRecordRangesByLevel(Integer.valueOf(2));
        }
        map.put(dataDictionary.getRecordCode(), ranges);
      }
    }
    return map;
  }

  public List getCustomers() {
    return this.customerDAO.getCustomers();
  }

  public List getNearViewCustomersByUserId(Integer userId) {
    if (userId == null) return null;
    Customer customer = new Customer();
    customer.setCategorys("1,3");
    customer.setSalePersonId(userId);
    customer.setOrderByViewDate("1");
    return this.customerDAO.getCustomersByDynamic(customer);
  }

  public List getNearViewCustomers(Integer userid, Page page, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    customer.setCategorys("1,3");
    customer.setSalePersonId(userid);
    customer.setOrderByViewDate("1");
    customer.setNearDate(DateUtils.getDateSubtractDay(new Date(), 30L));
    
    List<Customer> customers = this.customerDAO.getMyShareCustomer(customer);
    for(Customer cus : customers)
    {
        Users user = this.userDAO.getUser(cus.getSalePersonId());
        if (user != null) {
        	cus.setSalePerson(user.getUserName());
        }    	
    }
    return customers;
  }

  public Integer getNearViewCustomersCount(Integer userid, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    customer.setCategorys("1,3");
    customer.setSalePersonId(userid);
    customer.setNearDate(DateUtils.getDateSubtractDay(new Date(), 30L));
    return this.customerDAO.getMyShareCustomerCount(customer);
  }
  public List getSaleStageUpdateCustomersByUserId(Integer userId) {
    if (userId == null) return null;
    Customer customer = new Customer();
    customer.setCategorys("1,3");
    customer.setSalePersonId(userId);
    customer.setOrderBySaleStageDate("1");

    List customers = new ArrayList();
    customers = this.customerDAO.getCustomersByDynamic(customer);
    if (customers != null) {
      for (int i = 0; i < customers.size(); i++) {
        Customer cCustomer = new Customer();
        cCustomer = (Customer)customers.get(i);

        if ((cCustomer != null) && (cCustomer.getSaleStageId() != null)) {
          SaleStageSet saleStage = new SaleStageSet();
          saleStage = this.saleStageSetDAO.getSaleStageByStageCode(cCustomer.getSaleStageId().toString());
          if (saleStage != null) {
            cCustomer.setSaleStage(saleStage.getStageName());
          }
        }
      }
    }
    return customers;
  }

  public List getPageMyCustomers(String viewType, Integer salePersonId, Page page, QueryCustomer queryCustomer) {
    if (salePersonId == null) return null;
    Customer customer = new Customer();
    customer.setCustomerCategory("1");

    customer.setPersonId(salePersonId);
    if (!StringUtils.isEmpty(viewType)) {
      if ("1".equals(viewType)) {
        customer.setIsNewAdd(salePersonId.toString());
        customer.setIsNotSaleRecord(Integer.valueOf(1));
      } else if ("2".equals(viewType)) {
        customer.setIsFollow("1");
      }

    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerCode())) {
        customer.setCustomerCode(queryCustomer.getCustomerCode());
      }
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if ((queryCustomer.getIndustryId() != null) && (queryCustomer.getIndustryId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getAreaId() != null) && (queryCustomer.getAreaId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if ((queryCustomer.getCustomerSourceId() != null) && (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(0)) > 0)) {
        if (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(4)) == 0)
          customer.setIsOthersource("1");
        else {
          customer.setCustomerSourceId(queryCustomer.getCustomerSourceId());
        }
      }
      if (!StringUtils.isEmpty(queryCustomer.getRequireDesc())) {
        customer.setCustomerName(queryCustomer.getRequireDesc());
      }
      if ((queryCustomer.getStartCreateDate() != null) && (queryCustomer.getEndCreateDate() != null)) {
        customer.setStartCreateDate(queryCustomer.getStartCreateDate());
        customer.setEndCreateDate(queryCustomer.getEndCreateDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }

    }

    if ((queryCustomer != null) && 
      (!StringUtils.isEmpty(queryCustomer.getSortRecord())) && 
      (!StringUtils.isEmpty(queryCustomer.getSortWay()))) {
      customer.setSortRecord(queryCustomer.getSortRecord());
      customer.setSortWay(queryCustomer.getSortWay());
    }

    List customers = new ArrayList();
    customers = this.customerDAO.getPageCustomersByDynamic(customer, page);
    if (customers != null) {
      for (int i = 0; i < customers.size(); i++) {
        Customer cCustomer = new Customer();
        cCustomer = (Customer)customers.get(i);
        Users user = new Users();
        user = this.userDAO.getUser(cCustomer.getSalePersonId());
        if (user != null) {
          cCustomer.setSalePerson(user.getUserName());
        }

        cCustomer.setLinkNum(this.saleRecordDAO.getSaleRecordCountByCustomerId(cCustomer.getId()));

        if ((cCustomer != null) && (cCustomer.getSaleStageId() != null)) {
          SaleStageSet saleStage = new SaleStageSet();
          saleStage = this.saleStageSetDAO.getSaleStageByStageCode(cCustomer.getSaleStageId().toString());
          if (saleStage != null) {
            cCustomer.setSaleStage(saleStage.getStageName());
          }
        }

        CustomerShare customerShare = new CustomerShare();
        customerShare = this.customerShareDAO.getCustomerShareByCustomerIdAndSalePersonId(cCustomer.getId(), salePersonId);
        if ((customerShare != null) && ("1".equals(customerShare.getIsCurShare()))) {
          cCustomer.setIsWrite(customerShare.getIsReadWrite());
        }
      }
    }
    return customers;
  }

  public Integer getPageMyCustomersCount(String viewType, Integer salePersonId, QueryCustomer queryCustomer) {
    if (salePersonId == null) return null;
    Customer customer = new Customer();
    customer.setCustomerCategory("1");

    customer.setPersonId(salePersonId);
    if (!StringUtils.isEmpty(viewType)) {
      if ("1".equals(viewType)) {
        customer.setIsNewAdd(salePersonId.toString());
        customer.setIsNotSaleRecord(Integer.valueOf(1));
      } else if ("2".equals(viewType)) {
        customer.setIsFollow("1");
      }

    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerCode())) {
        customer.setCustomerCode(queryCustomer.getCustomerCode());
      }
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if ((queryCustomer.getIndustryId() != null) && (queryCustomer.getIndustryId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getAreaId() != null) && (queryCustomer.getAreaId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if ((queryCustomer.getCustomerSourceId() != null) && (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(0)) > 0)) {
        if (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(4)) == 0)
          customer.setIsOthersource("1");
        else {
          customer.setCustomerSourceId(queryCustomer.getCustomerSourceId());
        }
      }
      if (!StringUtils.isEmpty(queryCustomer.getRequireDesc())) {
        customer.setCustomerName(queryCustomer.getRequireDesc());
      }
      if ((queryCustomer.getStartCreateDate() != null) && (queryCustomer.getEndCreateDate() != null)) {
        customer.setStartCreateDate(queryCustomer.getStartCreateDate());
        customer.setEndCreateDate(queryCustomer.getEndCreateDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }

    return this.customerDAO.getPageCustomersCountByDynamic(customer);
  }

  public List getPageMyAllCustomers(String viewType, Integer salePersonId, Page page)
  {
    if (salePersonId == null) return null;
    Customer customer = new Customer();
    customer.setCategorys("1,3");

    customer.setPersonId(salePersonId);
    if (!StringUtils.isEmpty(viewType)) {
      if ("1".equals(viewType))
        customer.setIsNewAdd(salePersonId.toString());
      else if ("2".equals(viewType)) {
        customer.setIsFollow("1");
      }
    }
    List customers = new ArrayList();
    customers = this.customerDAO.getPageCustomersByDynamic(customer, page);
    if (customers != null) {
      for (int i = 0; i < customers.size(); i++) {
        Customer cCustomer = new Customer();
        cCustomer = (Customer)customers.get(i);
        Users user = new Users();
        user = this.userDAO.getUser(cCustomer.getSalePersonId());
        if (user != null) {
          cCustomer.setSalePerson(user.getUserName());
        }

        cCustomer.setLastLink(this.saleRecordDAO.getSaleRecordLastLinkByCustomerId(cCustomer.getId()));

        cCustomer.setLinkNum(this.saleRecordDAO.getSaleRecordCountByCustomerId(cCustomer.getId()));

        if ((cCustomer != null) && (cCustomer.getSaleStageId() != null)) {
          SaleStageSet saleStage = new SaleStageSet();
          saleStage = this.saleStageSetDAO.getSaleStageByStageCode(cCustomer.getSaleStageId().toString());
          if (saleStage != null) {
            cCustomer.setSaleStage(saleStage.getStageName());
          }
        }

        CustomerShare customerShare = new CustomerShare();
        customerShare = this.customerShareDAO.getCustomerShareByCustomerIdAndSalePersonId(cCustomer.getId(), salePersonId);
        if ((customerShare != null) && ("1".equals(customerShare.getIsCurShare()))) {
          cCustomer.setIsWrite(customerShare.getIsReadWrite());
        }
      }
    }
    return customers;
  }

  public Integer getPageMyAllCustomersCount(String viewType, Integer salePersonId)
  {
    if (salePersonId == null) return null;
    Customer customer = new Customer();
    customer.setCategorys("1,3");

    customer.setPersonId(salePersonId);
    if (!StringUtils.isEmpty(viewType)) {
      if ("1".equals(viewType))
        customer.setIsNewAdd(salePersonId.toString());
      else if ("2".equals(viewType)) {
        customer.setIsFollow("1");
      }
    }
    return this.customerDAO.getPageCustomersCountByDynamic(customer);
  }

  public List getPageAllCustomers(String customerName, Page page) {
    Customer customer = new Customer();
    customer.setCategorys("0,1,2,3,4,5,6,7");
    if (!StringUtils.isEmpty(customerName)) {
      customer.setCustomerName(customerName);
    }
    List customers = new ArrayList();
    customers = this.customerDAO.getPageCustomersByDynamic(customer, page);
    return customers;
  }

  public Integer getPageAllCustomerCount(String customerName) {
    Customer customer = new Customer();
    customer.setCategorys("0,1,2,3,4,5,6,7");
    if (!StringUtils.isEmpty(customerName)) {
      customer.setCustomerName(customerName);
    }
    return this.customerDAO.getPageCustomersCountByDynamic(customer);
  }

  public List getPageMyAllCustomersByCustomerName(String customerName, Integer salePersonId, Page page)
  {
    if (salePersonId == null) return null;
    Customer customer = new Customer();

    customer.setCategorys("1,3");

    customer.setPersonId2(salePersonId);
    if (!StringUtils.isEmpty(customerName)) {
      customer.setCustomerName(customerName);
    }
    List customers = new ArrayList();
    customers = this.customerDAO.getPageCustomersByDynamic(customer, page);
    if (customers != null) {
      for (int i = 0; i < customers.size(); i++) {
        Customer cCustomer = new Customer();
        cCustomer = (Customer)customers.get(i);
        CustomerShare customerShare = new CustomerShare();
        customerShare = this.customerShareDAO.getCustomerShareByCustomerIdAndSalePersonId(cCustomer.getId(), salePersonId);
        if ((customerShare != null) && ("1".equals(customerShare.getIsCurShare()))) {
          cCustomer.setIsWrite(customerShare.getIsReadWrite());
        }
      }
    }
    return customers;
  }

  public Integer getPageMyAllCustomersCountByCustomerName(String customerName, Integer salePersonId)
  {
    if (salePersonId == null) return null;
    Customer customer = new Customer();
    customer.setCategorys("1,3");

    customer.setPersonId2(salePersonId);
    if (!StringUtils.isEmpty(customerName)) {
      customer.setCustomerName(customerName);
    }
    return this.customerDAO.getPageCustomersCountByDynamic(customer);
  }

  public List getPageDeptCustomers(Integer userId, Page page, QueryCustomer queryCustomer) {
    if (userId == null) return null;

    Users user = new Users();
    user = this.userDAO.getUser(userId);
    if (user == null) return null;
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    Customer customer = new Customer();
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setCustomerCategory("1");
      customer.setDeptIds(deptIds);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerCode())) {
        customer.setCustomerCode(queryCustomer.getCustomerCode());
      }
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if ((queryCustomer.getIndustryId() != null) && (queryCustomer.getIndustryId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getAreaId() != null) && (queryCustomer.getAreaId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if ((queryCustomer.getSalePersonId() != null) && (queryCustomer.getSalePersonId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }
      if ((queryCustomer.getCustomerSourceId() != null) && (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(0)) > 0)) {
        if (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(4)) == 0)
          customer.setIsOthersource("1");
        else {
          customer.setCustomerSourceId(queryCustomer.getCustomerSourceId());
        }
      }
      if (!StringUtils.isEmpty(queryCustomer.getRequireDesc())) {
        customer.setCustomerName(queryCustomer.getRequireDesc());
      }
      if ((queryCustomer.getStartCreateDate() != null) && (queryCustomer.getEndCreateDate() != null)) {
        customer.setStartCreateDate(queryCustomer.getStartCreateDate());
        customer.setEndCreateDate(queryCustomer.getEndCreateDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }

    }

    if ((queryCustomer != null) && 
      (!StringUtils.isEmpty(queryCustomer.getSortRecord())) && 
      (!StringUtils.isEmpty(queryCustomer.getSortWay()))) {
      customer.setSortRecord(queryCustomer.getSortRecord());
      customer.setSortWay(queryCustomer.getSortWay());
    }

    List customers = new ArrayList();
    customers = this.customerDAO.getPageCustomersByDynamic(customer, page);
    if (customers != null) {
      for (int i = 0; i < customers.size(); i++) {
        Customer cCustomer = new Customer();
        cCustomer = (Customer)customers.get(i);
        user = new Users();
        user = this.userDAO.getUser(cCustomer.getSalePersonId());
        if (user != null) {
          cCustomer.setSalePerson(user.getUserName());
        }

        cCustomer.setLastLink(this.saleRecordDAO.getSaleRecordLastLinkByCustomerId(cCustomer.getId()));

        cCustomer.setLinkNum(this.saleRecordDAO.getSaleRecordCountByCustomerId(cCustomer.getId()));

        if ((cCustomer != null) && (cCustomer.getSaleStageId() != null)) {
          SaleStageSet saleStage = new SaleStageSet();
          saleStage = this.saleStageSetDAO.getSaleStageByStageCode(cCustomer.getSaleStageId().toString());
          if (saleStage != null) {
            cCustomer.setSaleStage(saleStage.getStageName());
          }
        }
      }
    }
    return customers;
  }

  public Integer getPageDeptCustomerCount(Integer userId, QueryCustomer queryCustomer) {
    if (userId == null) return null;

    Users user = new Users();
    user = this.userDAO.getUser(userId);
    if (user == null) return null;
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    Customer customer = new Customer();
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setCustomerCategory("1");
      customer.setDeptIds(deptIds);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerCode())) {
        customer.setCustomerCode(queryCustomer.getCustomerCode());
      }
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if ((queryCustomer.getIndustryId() != null) && (queryCustomer.getIndustryId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getAreaId() != null) && (queryCustomer.getAreaId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if ((queryCustomer.getSalePersonId() != null) && (queryCustomer.getSalePersonId().compareTo(Integer.valueOf(0)) > 0)) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }
      if ((queryCustomer.getCustomerSourceId() != null) && (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(0)) > 0)) {
        if (queryCustomer.getCustomerSourceId().compareTo(Integer.valueOf(4)) == 0)
          customer.setIsOthersource("1");
        else {
          customer.setCustomerSourceId(queryCustomer.getCustomerSourceId());
        }
      }
      if (!StringUtils.isEmpty(queryCustomer.getRequireDesc())) {
        customer.setCustomerName(queryCustomer.getRequireDesc());
      }
      if ((queryCustomer.getStartCreateDate() != null) && (queryCustomer.getEndCreateDate() != null)) {
        customer.setStartCreateDate(queryCustomer.getStartCreateDate());
        customer.setEndCreateDate(queryCustomer.getEndCreateDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }

    return this.customerDAO.getPageCustomersCountByDynamic(customer);
  }

  public Customer getCustomerById(Integer id) {
    return this.customerDAO.getCustomerById(id);
  }

  public Customer getCustomerById2(Integer id, String realPath, Integer userId) {
    Customer customer = new Customer();
    customer = this.customerDAO.getCustomerById(id);
    if (customer == null) return null;
    customer.setSalePerson(this.userDAO.getUserNameById(customer.getSalePersonId()));
    customer.setShareSalePersons(this.userDAO.getShareUsersByCustomerId(customer.getId(), customer.getSalePersonId()));

    customer.setLastLink(this.saleRecordDAO.getSaleRecordLastLinkByCustomerId(customer.getId()));
    customer.getLinkmans().addAll(this.linkmanDAO.getLinkmansByCustomerId(customer.getId()));

    CustomerShare customerShare = new CustomerShare();
    customerShare = this.customerShareDAO.getCustomerShareByCustomerIdAndSalePersonId(customer.getId(), userId);
    if ((customerShare != null) && ("1".equals(customerShare.getIsCurShare()))) {
      customer.setIsWrite(customerShare.getIsReadWrite());
    }

    List saleRecords = new ArrayList();

    saleRecords = this.saleRecordDAO.getSaleRecordsByCustomerIdAndStatus(customer.getId(), "0");
    customer.getSaleRecords().addAll(saleRecords);
    if ((saleRecords != null) && (saleRecords.size() > 0)) {
      for (int i = 0; i < saleRecords.size(); i++) {
        SaleRecord saleRecord = (SaleRecord)saleRecords.get(i);
        List systemFiles = new ArrayList();
        systemFiles = this.systemFileDAO.getSystemFilesByRelatedId(saleRecord.getId());
        if ((systemFiles != null) && (!StringUtils.isEmpty(realPath))) {
          for (int j = 0; j < systemFiles.size(); j++) {
            SystemFile systemFile = new SystemFile();
            systemFile = (SystemFile)systemFiles.get(j);
            systemFile.setPerson(this.userDAO.getUserNameById(systemFile.getPersonId()));
            if (!this.saleRecordDAO.isFileExists(null, realPath + systemFile.getPath(), customer.getId(), saleRecord.getId(), systemFile.getFileName()))
              continue;
            customer.getSaleRecordAddeds().add(systemFile);
          }
        }
      }
    }

    return customer;
  }

  public CustomerLimitForm getCustomerLimit(Integer salePersonId) {
    if (salePersonId == null) return null;
    CustomerLimitForm customerLimitForm = new CustomerLimitForm();
    Customer customer = new Customer();
    customer.setMyCustomers(Integer.valueOf(1));
    customer.setSalePersonId(salePersonId);
    Integer totalCustomerLimit = this.manageSetDAO.getSalePersonCustomerLimit(salePersonId);
    Integer currentCustomerNum = this.customerDAO.getCustomersCountByDynamic(customer);
    customerLimitForm.setTotalCustomerLimit(totalCustomerLimit.intValue());
    customerLimitForm.setCurrentCustomerNum(currentCustomerNum.intValue());
    customerLimitForm.setUntappedCustomerNum(totalCustomerLimit.intValue() - currentCustomerNum.intValue());
    return customerLimitForm;
  }

  public CustomerLimitForm getDiscardCustomerNum(Integer salePersonId) {
    if (salePersonId == null) return null;
    CustomerLimitForm customerLimitForm = new CustomerLimitForm();

    Customer customer = new Customer();
    customer.setMyCustomers(Integer.valueOf(1));
    customer.setSalePersonId(salePersonId);
    Integer totalCustomerLimit = this.manageSetDAO.getSalePersonCustomerLimit(salePersonId);
    Integer discardCustmerNum = new Integer(0);

    int isUseCommon = 0;
    isUseCommon = this.manageSetDAO.isUseCommons();
    if (isUseCommon != 0)
    {
      discardCustmerNum = Integer.valueOf(Double.valueOf(totalCustomerLimit.intValue() * 0.3D).intValue());
    }
    else {
      discardCustmerNum = Integer.valueOf(Double.valueOf(totalCustomerLimit.intValue() * 0.2D).intValue());
    }

    customer = new Customer();
    customer.setCustomerCategory("5");
    customer.setSalePersonId(salePersonId);
    Integer currentDiscardNum = this.customerDAO.getCustomersCountByDynamic(customer);

    customerLimitForm.setTotalCustomerLimit(discardCustmerNum.intValue());
    customerLimitForm.setCurrentCustomerNum(currentDiscardNum.intValue());
    customerLimitForm.setUntappedCustomerNum(discardCustmerNum.intValue() - currentDiscardNum.intValue());

    return customerLimitForm;
  }

  public Customer getCustomerSaleInfoById(Integer id)
  {
    if (id == null) return null;

    return this.customerDAO.getCustomerSaleInfoById(id);
  }

  public List isRepeatSet() {
    List manageSetObjects = new ArrayList();

    ManageSet mSet = new ManageSet();
    ManageSet manageSet = new ManageSet();
    manageSet.setMType("5");
    mSet = this.manageSetDAO.getManageSetByTypeStatus(manageSet);
    if ((mSet != null) && ("0".equals(mSet.getStatus()))) {
      manageSetObjects = this.manageSetDAO.getManageSetObjectByManageSetId(mSet.getId());
    }

    return manageSetObjects;
  }

  public boolean isRepeatAddCustomer(Customer customer) {
    if (customer == null) return false;

    List list = new ArrayList();
    list = isRepeatSet();
    if (list == null) return false;
    Customer rCustomer = new Customer();
    for (int i = 0; i < list.size(); i++) {
      ManageSetObject manageSetObject = new ManageSetObject();
      manageSetObject = (ManageSetObject)list.get(i);
      if (manageSetObject.getOtherId().intValue() == 2)
        rCustomer.setCustomerName(customer.getCustomerName());
      else if (manageSetObject.getOtherId().intValue() == 3)
        rCustomer.setAddress(customer.getAddress());
      else if (manageSetObject.getOtherId().intValue() == 14)
        rCustomer.setHttp(customer.getHttp());
      else if (manageSetObject.getOtherId().intValue() == 4)
        rCustomer.setPhone(customer.getPhone());
      else if (manageSetObject.getOtherId().intValue() == 17)
        rCustomer.setCustomerSimple(customer.getCustomerSimple());
      else if (manageSetObject.getOtherId().intValue() == 5) {
        rCustomer.setFax(customer.getFax());
      }
    }
    list = new ArrayList();
    rCustomer.setSignCategory(Integer.valueOf(9));

    list = this.customerDAO.getCustomers();
    for(int i=0; i<list.size(); i++)
    {
    	Customer c = (Customer)list.get(i);
    	if(customer.getCustomerName().indexOf(c.getCustomerName()) > 0)
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    return (list != null) && (list.size() > 0);
  }

  public boolean isRepeatUpdateCustomer(Customer customer)
  {
    if (customer == null) return false;

    Customer oldCustomer = new Customer();
    oldCustomer = this.customerDAO.getCustomerById(customer.getId());
    if (oldCustomer == null) return false;

    List list = new ArrayList();
    list = isRepeatSet();
    if (list == null) return false;

    Customer rCustomer = new Customer();
    int sign = 0;
    for (int i = 0; i < list.size(); i++) {
      ManageSetObject manageSetObject = new ManageSetObject();
      manageSetObject = (ManageSetObject)list.get(i);
      if (manageSetObject.getOtherId().intValue() == 2) {
        rCustomer.setCustomerName(customer.getCustomerName());
        if (!oldCustomer.getCustomerName().equals(customer.getCustomerName())) continue; sign++;
      } else if (manageSetObject.getOtherId().intValue() == 3) {
        rCustomer.setAddress(customer.getAddress());
        if (!oldCustomer.getAddress().equals(customer.getAddress())) continue; sign++;
      } else if (manageSetObject.getOtherId().intValue() == 14) {
        rCustomer.setHttp(customer.getHttp());
        if (!oldCustomer.getHttp().equals(customer.getHttp())) continue; sign++;
      } else if (manageSetObject.getOtherId().intValue() == 4) {
        rCustomer.setPhone(customer.getPhone());
        if (!oldCustomer.getPhone().equals(customer.getPhone())) continue; sign++;
      } else if (manageSetObject.getOtherId().intValue() == 17) {
        rCustomer.setCustomerSimple(customer.getCustomerSimple());
        if (!oldCustomer.getCustomerSimple().equals(customer.getCustomerSimple())) continue; sign++;
      } else if (manageSetObject.getOtherId().intValue() == 5) {
        rCustomer.setFax(customer.getFax());
        if (!oldCustomer.getFax().equals(customer.getFax())) continue; sign++;
      }
    }
    if (sign == list.size()) return false;
    list = new ArrayList();
    rCustomer.setSignCategory(Integer.valueOf(9));

    list = this.customerDAO.getRepeatCustomersByDynamic(rCustomer);
    return (list != null) && (list.size() > 0);
  }

  public boolean insertCustomer(Customer customer)
  {
    if (customer == null) return false;
    boolean result = false;
    customer.setCustomerCategory("1");
    customer.setIsNewAdd(customer.getSalePersonId().toString());
    customer.setIsFollow("0");
    customer.setIsShare("0");
    customer.setCreateDate(new Date());
    customer.setUpdateCustomerDate(new Date());
    customer.setUpdateSaleRecordDate(new Date());
    customer.setUpdateSaleStageDate(new Date());
    customer.setSaleStageId(Integer.valueOf(0));
    Users user = new Users();
    user = this.userDAO.getUser(customer.getSalePersonId());
    if (user != null) {
      customer.setDeptId(user.getDeptId());
    }
    result = this.customerDAO.insertCustomer(customer);

    Customer customer2 = new Customer();
    customer2 = this.customerDAO.getCustomerByCustomerCode(customer.getCustomerCode());
    if (customer2 != null) {
      CustomerShare customerShare = new CustomerShare();
      customerShare.setCustomerId(customer2.getId());
      customerShare.setSalePersonId(customer2.getSalePersonId());
      customerShare.setIsCurShare("1");
      customerShare.setIsReadWrite("1");
      this.customerShareDAO.insertCustomerShare(customerShare);

      customer2.setCustomerId(customer2.getId());
      customer2.setUpdatePersonId(customer2.getSalePersonId());
      customer2.setUpdateDate(new Date());
      customer2.setIsPrimitive("1");
      this.customerUpdateHistoryDAO.insertCustomerUpdateHistory(customer2);
    }

    return result;
  }

  public boolean updateCustomer(Customer customer, String realPath) 
  {
    if (customer == null) return false;

    boolean result = false;

    customer.setUpdateCustomerDate(new Date());
    
    MultipartFile multipartFile = customer.getContents();
    if ((!StringUtils.isEmpty(realPath)) && (multipartFile != null) && (!multipartFile.isEmpty()))
    {
	      String fileName = multipartFile.getOriginalFilename();
	      String storedPath = getStoredPath(realPath, customer.getId(), 0) + System.getProperty("file.separator") + fileName;
	      boolean result2 = false;
	      result2 = FileUtils.saveFile(storedPath, multipartFile);
	      if (result2) 
	      {
	          SystemFile systemFile = new SystemFile();
	          systemFile.setFileName(fileName);
	          systemFile.setFileType(multipartFile.getContentType());

	          systemFile.setPath(getStoredRelativePath(customer.getId(), 0) + System.getProperty("file.separator"));
	          systemFile.setPersonId(customer.getPersonId());
	          systemFile.setRelatedId(customer.getId());
	          systemFile.setRelatedTable("customer");
	          systemFile.setSize(multipartFile.getSize()+"");
	          systemFile.setUploadDate(new Date());
	          this.systemFileDAO.insertSystemFile(systemFile);
	       }	      
	      customer.setLicenceUrl(storedPath);
	}    
    result = this.customerDAO.updateCustomerByStaticParam(customer);

    Customer customer2 = new Customer();
    customer2 = this.customerDAO.getCustomerById(customer.getId());
    if (customer2 != null) {
      customer2.setCustomerId(customer2.getId());
      customer2.setUpdatePersonId(customer.getCurrentUserId());
      customer2.setUpdateDate(new Date());
      customer2.setIsPrimitive("0");
      this.customerUpdateHistoryDAO.insertCustomerUpdateHistory(customer2);
    }

    return result;
  }

  public boolean updateViewCustomerDate(Integer customerId) {
    if (customerId == null) return false;
    Customer customer = new Customer();
    customer.setId(customerId);
    customer.setViewDate(new Date());
    return this.customerDAO.updateCustomer(customer);
  }

  public boolean discardMyCustomers(String[] ids) {
    if ((ids == null) || (ids.length <= 0)) return false;

    Customer customer = new Customer();
    customer.setCustomerCategory("5");

    for (int i = 0; i < ids.length; i++) {
      boolean result = false;
      customer.setId(Integer.valueOf(Integer.parseInt(ids[i])));
      result = this.customerDAO.updateCustomer(customer);

      if (result) {
        this.customerShareDAO.deleteCustomerShare(Integer.valueOf(Integer.parseInt(ids[i])));
        this.customerDAO.updateCustomerShare(Integer.valueOf(Integer.parseInt(ids[i])), "0");
      }
    }

    return true;
  }

  public List getDeptNameCustomer(Integer userid, Page page, String customerName, QueryCustomer queryCustomer) {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    customer.setCustomerSourceId(Integer.valueOf(2));
    if (!StringUtils.isEmpty(customerName)) {
      if (customerName == "客户名称") {
        customerName = "";
      }
      customer.setCustomerName(customerName);
    }
    if (queryCustomer != null)
    {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSalePersonId1() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId1());
      }
      else if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }

      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    Users user = new Users();
    user = this.userDAO.getUser(userid);
    if (user == null) return null;
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setDeptIds(deptIds);
    }
    return this.customerDAO.getDeptNameCustomer(customer);
  }

  public Integer getDeptNameCustomerCount(Integer userid, String customerName, QueryCustomer queryCustomer) {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setCustomerSourceId(Integer.valueOf(2));
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSalePersonId1() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId1());
      }
      else if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }

      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    Users user = new Users();
    user = this.userDAO.getUser(userid);
    if (user == null) return null;
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setDeptIds(deptIds);
    }
    return this.customerDAO.getDeptNameCustomerCount(customer);
  }

  public List getOwnNameCustomer(Integer userid, Page page, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    customer.setCustomerSourceId(Integer.valueOf(2));
    customer.setSalePersonId(userid);
    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    
    List<Customer> customers = this.customerDAO.getOwnNameCustomer(customer);
    for(Customer cus : customers)
    {
        Users user = this.userDAO.getUser(cus.getSalePersonId());
        if (user != null) {
        	cus.setSalePerson(user.getUserName());
        }    	
    }
    return customers;
  }

  public Integer getOwnNameCustomerCount(Integer userid, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    customer.setCustomerSourceId(Integer.valueOf(2));
    customer.setSalePersonId(userid);
    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getOwnNameCustomerCount(customer);
  }

  public List getMyShareCustomer(Integer userid, Page page, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    customer.setIsShare("1");
    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSalePersonId1() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId1());
      }
      else if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }

      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getMyShareCustomer(customer);
  }

  public Integer getMyShareCustomerCount(Integer userid, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    customer.setIsShare("1");
    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSalePersonId1() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId1());
      }
      else if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }

      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getMyShareCustomerCount(customer);
  }

  public List getNearUpdatedCustomer(Integer userid, Page page, String customerName, QueryCustomer queryCustomer) {
    if (userid == null) return null;
    Customer customer = new Customer();
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    customer.setCategorys("1,3");
    customer.setSalePersonId(userid);
    customer.setOrderByUpdateCustomerDate("1");
    customer.setNearUpdateDate(DateUtils.getDateSubtractDay(new Date(), 30L));
    
    List<Customer> customers = this.customerDAO.getMyShareCustomer(customer);
    for(Customer cus : customers)
    {
        Users user = this.userDAO.getUser(cus.getSalePersonId());
        if (user != null) {
        	cus.setSalePerson(user.getUserName());
        }    	
    }
    return customers;
  }

  public Integer getNearUpdatedCustomerCount(Integer userid, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    customer.setCategorys("1,3");
    customer.setSalePersonId(userid);
    customer.setNearUpdateDate(DateUtils.getDateSubtractDay(new Date(), 30L));
    return this.customerDAO.getMyShareCustomerCount(customer);
  }

  public List getDeptSaleRecordCustomer(Integer userid, Page page, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    Users user = new Users();
    user = this.userDAO.getUser(userid);
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setDeptIds(deptIds);
    }
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getSalePersonId1() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId1());
      }
      else if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }

      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    customer.setCategorys("1,3");
    customer.setIsSaleRecord(Integer.valueOf(1));
    return this.customerDAO.getMyShareCustomer(customer);
  }

  public Integer getDeptSaleRecordCustomerCount(Integer userid, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setCategorys("1,3");
    customer.setIsSaleRecord(Integer.valueOf(1));
    Users user = new Users();
    user = this.userDAO.getUser(userid);
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setDeptIds(deptIds);
    }
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getSalePersonId1() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId1());
      }
      else if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }

      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getMyShareCustomerCount(customer);
  }

  public List getDeptSucceedContract(Integer userid, Page page, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    Users user = new Users();
    user = this.userDAO.getUser(userid);
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());

    if (!StringUtils.isEmpty(deptIds)) {
      customer.setDeptIds(deptIds);
    }
    customer.setSaleStatus("3");
    customer.setIsSaleRecord(Integer.valueOf(1));
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (!StringUtils.isEmpty(queryCustomer.getContractCode())) {
        customer.setContractCode(queryCustomer.getContractCode());
      }
      if (queryCustomer.getSalePersonId1() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId1());
      }
      else if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }

      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getDeptSucceedContract(customer);
  }

  public Integer getDeptSucceedContractCount(Integer userid, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    Users user = new Users();
    user = this.userDAO.getUser(userid);
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setDeptIds(deptIds);
    }
    customer.setSaleStatus("3");
    customer.setIsSaleRecord(Integer.valueOf(1));
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (!StringUtils.isEmpty(queryCustomer.getContractCode())) {
        customer.setContractCode(queryCustomer.getContractCode());
      }
      if (queryCustomer.getSalePersonId1() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId1());
      }
      else if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }

      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getDeptSucceedContractCount(customer);
  }

  public List getMySucceedContract(Integer userid, Page page, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    customer.setSalePersonId(userid);
    customer.setSaleStatus("3");
    customer.setIsSaleRecord(Integer.valueOf(1));
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (!StringUtils.isEmpty(queryCustomer.getContractCode())) {
        customer.setContractCode(queryCustomer.getContractCode());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getDeptSucceedContract(customer);
  }

  public Integer getMySucceedContractCount(Integer userid, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setSalePersonId(userid);
    customer.setSaleStatus("3");
    customer.setIsSaleRecord(Integer.valueOf(1));
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (!StringUtils.isEmpty(queryCustomer.getContractCode())) {
        customer.setContractCode(queryCustomer.getContractCode());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getDeptSucceedContractCount(customer);
  }

  public List getDeptHangupBusinessOpportunity(Integer userid, Page page, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    customer.setSaleStatus("1,2,21");
    Users user = new Users();
    user = this.userDAO.getUser(userid);
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setDeptIds(deptIds);
    }
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSalePersonId1() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId1());
      }
      else if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }

      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getMyShareCustomer(customer);
  }

  public Integer getDeptHangupBusinessOpportunityCount(Integer userid, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setSaleStatus("1,2,21");
    Users user = new Users();
    user = this.userDAO.getUser(userid);
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setDeptIds(deptIds);
    }
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSalePersonId1() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId1());
      }
      else if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }

      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getMyShareCustomerCount(customer);
  }

  public List getMyHangupBusinessOpportunity(Integer userid, Page page, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    customer.setSaleStatus("1,2,21");
    customer.setSalePersonId(userid);
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getMyShareCustomer(customer);
  }

  public Integer getMyHangupBusinessOpportunityCount(Integer userid, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setSaleStatus("1,2,21");
    customer.setSalePersonId(userid);
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getMyShareCustomerCount(customer);
  }

  public List getDeptGiveupBusinessOpportunity(Integer userid, Page page, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    customer.setSaleStatus("4");
    Users user = new Users();
    user = this.userDAO.getUser(userid);
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setDeptIds(deptIds);
    }
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSalePersonId1() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId1());
      }
      else if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }

      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getMyShareCustomer(customer);
  }

  public Integer getDeptGiveupBusinessOpportunityCount(Integer userid, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setSaleStatus("4");
    Users user = new Users();
    user = this.userDAO.getUser(userid);
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setDeptIds(deptIds);
    }
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSalePersonId1() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId1());
      }
      else if (queryCustomer.getSalePersonId() != null) {
        customer.setSalePersonId(queryCustomer.getSalePersonId());
      }

      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartPredictContractDate() != null) && (queryCustomer.getEndPredictContractDate() != null)) {
        customer.setStartPredictContractDate(queryCustomer.getStartPredictContractDate());
        customer.setEndPredictContractDate(queryCustomer.getEndPredictContractDate());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getMyShareCustomerCount(customer);
  }

  public List getMyGiveupBusinessOpportunity(Integer userid, Page page, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    customer.setSaleStatus("4");
    customer.setSalePersonId(userid);
    customer.setIsSaleRecord(Integer.valueOf(1));
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }

      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getMyShareCustomer(customer);
  }

  public Integer getMyGiveupBusinessOpportunityCount(Integer userid, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setSaleStatus("4");
    customer.setSalePersonId(userid);
    customer.setIsSaleRecord(Integer.valueOf(1));
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }

      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getMyShareCustomerCount(customer);
  }

  public List getNearChangedBusinessOpportunity(Integer userid, Page page, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    customer.setCategorys("1,3");
    Users user = new Users();
    user = this.userDAO.getUser(userid);
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setDeptIds(deptIds);
    }
    customer.setOrderBySaleStageDate("1");
    customer.setNearUpdateSaleStageDate(DateUtils.getDateSubtractDay(new Date(), 30L));
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getMyShareCustomer(customer);
  }

  public Integer getNearChangedBusinessOpportunityCount(Integer userid, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setCategorys("1,3");
    Users user = new Users();
    user = this.userDAO.getUser(userid);
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setDeptIds(deptIds);
    }
    customer.setOrderBySaleStageDate("1");
    customer.setNearUpdateSaleStageDate(DateUtils.getDateSubtractDay(new Date(), 30L));
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getMyShareCustomerCount(customer);
  }

  public List getNearFollowedBusinessOpportunity(Integer userid, Page page, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setPageStartRow(page.getPageStartRow());
    customer.setPageEndRow(page.getPageEndRow());
    customer.setCategorys("1,3");
    Users user = new Users();
    user = this.userDAO.getUser(userid);
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setDeptIds(deptIds);
    }
    customer.setOrderByUpdateSaleRecordDate("1");
    customer.setIsSaleRecord(Integer.valueOf(1));
    customer.setNearUpdateSaleRecordDate(DateUtils.getDateSubtractDay(new Date(), 30L));
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getMyShareCustomer(customer);
  }

  public Integer getNearFollowedBusinessOpportunityCount(Integer userid, String customerName, QueryCustomer queryCustomer)
  {
    if (userid == null) return null;
    Customer customer = new Customer();
    customer.setCategorys("1,3");
    Users user = new Users();
    user = this.userDAO.getUser(userid);
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setDeptIds(deptIds);
    }
    customer.setOrderByUpdateSaleRecordDate("1");
    customer.setIsSaleRecord(Integer.valueOf(1));
    customer.setNearUpdateSaleRecordDate(DateUtils.getDateSubtractDay(new Date(), 30L));
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      customer.setCustomerName(customerName);
    }

    if (queryCustomer != null) {
      if (!StringUtils.isEmpty(queryCustomer.getCustomerName())) {
        customer.setCustomerName(queryCustomer.getCustomerName());
      }
      if (queryCustomer.getSaleStageId() != null) {
        customer.setSaleStageId(queryCustomer.getSaleStageId());
      }
      if (queryCustomer.getAreaId() != null) {
        customer.setAreaId(queryCustomer.getAreaId());
      }
      if (queryCustomer.getIndustryId() != null) {
        customer.setIndustryId(queryCustomer.getIndustryId());
      }
      if ((queryCustomer.getStartLastLinkDate() != null) && (queryCustomer.getEndLastLinkDate() != null)) {
        customer.setStartLastLinkDate(queryCustomer.getStartLastLinkDate());
        customer.setEndLastLinkDate(queryCustomer.getEndLastLinkDate());
      }
    }
    return this.customerDAO.getMyShareCustomerCount(customer);
  }
  
  
  public String getStoredPath(String basePath, Integer customerId, Integer contractId)
  {
    String storedPath = basePath + "upload" + System.getProperty("file.separator") + "customer" + System.getProperty("file.separator") + customerId;
    File file = new File(storedPath);
    if ((file.exists()) && (file.isDirectory()))
    {
      return storedPath;
    }
    file.mkdirs();
    return storedPath;
  }  
  
  public String getStoredRelativePath(Integer customerId, Integer contractId)
  {
    String storedPath = "upload" + System.getProperty("file.separator") + "customer" + System.getProperty("file.separator") + customerId;
    File file = new File(storedPath);
    if ((file.exists()) && (file.isDirectory()))
    {
      return storedPath;
    }
    file.mkdirs();
    return storedPath;
  } 
  
  
	public List getCustomerAddedsById(String realPath, Integer id)
	{
		if (id == null)
			return null;
		if (StringUtils.isEmpty(realPath))
			return null;
		Customer customer = this.customerDAO.getCustomerById(id);
		if (customer == null)
			return null;

		List systemFiles = this.systemFileDAO.getSystemFilesByRelatedId(id);
		if (systemFiles != null) {
			for (int i = 0; i < systemFiles.size(); i++) {
				SystemFile systemFile = new SystemFile();
				systemFile = (SystemFile) systemFiles.get(i);
				systemFile.setPerson(this.userDAO.getUserNameById(systemFile
						.getPersonId()));
//				if (isFileExists(null, realPath + systemFile.getPath(),
//						contract.getCustomerId(), id, systemFile.getFileName())) {
//					contract.getSystemFiles().add(systemFile);
//				}
			}
		}
		return systemFiles;
	}
//	
	public List<ContractCaseType> getPageAllCaseTypes(String caseTypeName, Page page) {
		ContractCaseType caseType = new ContractCaseType();
		if(!StringUtils.isEmpty(caseTypeName)){
			caseType.setType(caseTypeName);
		}
		List<ContractCaseType> caseTypes = new ArrayList<ContractCaseType>();
		caseTypes = customerDAO.getPageCaseTypesByDynamic(caseType, page);
		return caseTypes;
	}
	
	public Integer getPageAllCaseTypeCount(String caseTypeName) {
		ContractCaseType caseType = new ContractCaseType();
		if(!StringUtils.isEmpty(caseTypeName)){
			caseType.setType(caseTypeName);
		}
		return customerDAO.getPageCaseTypeCountByDynamic(caseType);
	}
	
	public Integer getPageAllProcessGroupCount(String processGroupId) {
		ProcessGroup processGroup = new ProcessGroup();
		if(!StringUtils.isEmpty(processGroupId)){
			processGroup.setId(Integer.parseInt(processGroupId));
		}
		return customerDAO.getPageProcessGroupCountByDynamic(processGroup);
	}
	
	public List<ProcessGroup> getPageAllProcessGroup(String processGroupId, Page page) {
		ProcessGroup processGroup = new ProcessGroup();
		if(!StringUtils.isEmpty(processGroupId)){
			processGroup.setId(Integer.parseInt(processGroupId));
		}
		List<ProcessGroup> processGroupList = new ArrayList<ProcessGroup>();
		processGroupList = customerDAO.getPageProcessGroupByDynamic(processGroup, page);
		return processGroupList;
	}	
}